package com.jhws.business.property.controller;

import com.jhws.business.property.bo.PropertyAppModule;
import com.jhws.business.property.bo.PropertyRole;
import com.jhws.business.property.service.PropertyService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/property/app")
public class PropertyAppController {

    @Resource
    PropertyService IPropertyService;
    @Resource
    ITblUserDimService IUserService;

    @RequestMapping("module/save.x")
    @ResponseBody
    public JsonRespData saveAppModule(PropertyAppModule am){
        IPropertyService.save(am);
        return new JsonRespData("",1,"");
    }

    @RequestMapping("module/query.x")
    @ResponseBody
    public JsonRespData queryAppModule(){
        return new JsonRespData("",1,IPropertyService.getAppModuleList());
    }

    @RequestMapping("module/delete.x")
    @ResponseBody
    public JsonRespData deleteModuleById(String id){
        IPropertyService.deleteAppModuleById(id);
        return new JsonRespData("",1,"");
    }

    @RequestMapping("role/save.x")
    @ResponseBody
    public JsonRespData saveAppRole(PropertyRole role){
        TblUserDimBo user = IUserService.currentUser();
        role.setOwner(user.getLogName());
        IPropertyService.save(role);
        return new JsonRespData("",1,"");
    }

    @RequestMapping("role/queryPage.x")
    @ResponseBody
    public JsonRespData queryRolePage(){
        return new JsonRespData("",1,IPropertyService.getAppRoleList());
    }

    @RequestMapping("role/queryAll.x")
    @ResponseBody
    public JsonRespData queryRoleAll(){
        return new JsonRespData("",1,IPropertyService.getAppRoleAll());
    }

    @RequestMapping("role/getPerms.x")
    @ResponseBody
    public JsonRespData getAppRole(Long id){
        PropertyRole role = IPropertyService.getAppRole(id);
        String [] ids = role.getAppPermIds().split(",");
        String idsStr = "";
        for(int i=0;i<ids.length;i++){
            idsStr += "'"+ids[i]+"'";
            if(i+1 != ids.length)
                idsStr+=",";
        }
        if(StringUtils.isNotEmpty(idsStr)){
            return new JsonRespData("",1,IPropertyService.findAppModulesByWhere("where id in ("+idsStr+")"));
        }
        return new JsonRespData("",1,"");
    }

    @RequestMapping("role/delete.x")
    @ResponseBody
    public JsonRespData deleteRole(Long id){
        IPropertyService.deleteAppRole(id);
        return new JsonRespData("",1,"");
    }
}
